let Api = require('../../utils/api.js');
let wxRequest = require('../../utils/wxRequest.js');

Page({
  data: {
    user: {},
    isshowsafe: false,
    isshowclose: false,
  },
  onLoad: function (options) {
    let user = wx.getStorageSync('user')
    if (user) {
      this.setData({
        user: user
      })
    } else {
      wx.navigateTo({
        url: '../login/login',
      })
    }
  },
  safetap: function () {
    this.setData({
      isshowsafe: true
    })
  },
  closetap: function () {
    this.setData({
      isshowclose: true
    })
  },
  cancelsafetap: function () {
    this.setData({
      isshowsafe: false
    })
  },
  cancelclosetap: function () {
    this.setData({
      isshowclose: false
    })
  },
  logouttap: function () {
    wx.showModal({
      title: '提示',
      content: '您确定要退出吗?',
      confirmText: '确定',
      success(res) {
        if (res.confirm) {
          //清除用户信息
          wx.removeStorageSync('user')
          //回到登陆页面
          wx.reLaunch({
            url: '../login/login',
          })
        }
      }
    })
  },
  safeformsubmit: function (e) {
    let self = this
    let detail = e.detail.value
    let username = detail.username
    let password = detail.password
    let paramurl = "fg=1&name=" + username + "&pwd=" + password + "&hd="
    let token = ''
    wx.showLoading({
      title: '正在加载...',
    })
    wxRequest.getRequest(Api.agXgzl(token, paramurl), null).then(res => {
      if (res.data) {
        if (res.data == 1) {
          wx.showModal({
            title: '提示',
            content: '保存成功!',
          })
          self.setData({
            isshowsafe: false
          })
        }
      } else {
        wx.showModal({
          title: '提示',
          content: '保存失败!',
        })
      }
    }).finally(fina => {
      wx.hideLoading()
    })
  },
  closeformsubmit: function (e) {
    let self = this
    let detail = e.detail.value
    let bname = detail.bname
    let bank = detail.bank
    let bankno = detail.bankno
    let baddr = detail.baddr
    let btel = detail.btel
    let token = ''
    let paramurl = "fg=2&bank=" + bank + "&bankno=" + bankno + "&baddr=" + baddr + "&tel=" + btel + "&buser=" + bname
    wx.showLoading({
      title: '正在加载...',
    })
    wxRequest.getRequest(Api.agXgzl(token, paramurl), null).then(res => {
      if (res.data) {
        if (res.data == 1) {
          wx.showModal({
            title: '提示',
            content: '保存成功!',
          })
          self.setData({
            isshowclose: false
          })
        }
      } else {
        wx.showModal({
          title: '提示',
          content: '保存失败!',
        })
      }
    }).finally(fina => {
      wx.hideLoading()
    })
  }
})